package com.pet.shop.controller.advert;

import com.pet.config.Constants;
import com.pet.shop.controller.BaseController;
import com.pet.shop.entity.shop.AdvertInfo;
import com.pet.shop.service.IBaseService;
import com.pet.shop.service.shop.IAdvertInfoService;
import com.pet.shop.utils.ApiResultUtil;
import com.pet.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.*;

@RestController
@RequestMapping("/api/advert")
public class AdvertApiController{

    @Autowired
    private IAdvertInfoService advertInfoService;

    @Value("${fileUploadPath}")
    private String fileUploadPath;
    @Value("${fileContextPath}")
    private String fileContextPath;
    @Value("${fileViewServer}")
    private String fileViewServer;
    @Value("${fileDefaultAdvert}")
    private String fileDefaultAdvert;

    @RequestMapping("home")
    public Map<String, Object> home(){
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("type", Constants.INDEX_TYPE);
        List<AdvertInfo> advertInfoList = advertInfoService.queryAdvertInfo(paramMap);
        if(advertInfoList == null) advertInfoList = new ArrayList<>();
        advertInfoList.forEach(advert -> {
            if(StringUtils.isEmpty(advert.getPicUrl())){
                advert.setPicUrl(fileViewServer + fileDefaultAdvert);
            }else if(!advert.getPicUrl().startsWith("http")){
                advert.setPicUrl(fileViewServer + advert.getPicUrl());
            }
        });
        return ApiResultUtil.success_data(advertInfoList);
    }


}
